package com.qdxht.dispenser.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author GJ
 * @Description:
 * @date 2022/7/12 15:15
 */
public class DateUtil {



    /**
     * 返回年月日
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getFormatDate() {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = new Date(System.currentTimeMillis());
        String startDateString = format1.format(date);
        return startDateString;
    }

    /**
     * 返回年月日
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getFormatDate(Long time) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(time);
        String startDateString = format1.format(date);
        return startDateString;
    }

    /**
     * 返回年月日
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getFormat(Integer time) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time * 1000L);
        String startDateString = format1.format(date);
        return startDateString;
    }

    public static Date getFormatDateStr(String time) {
        SimpleDateFormat SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try{
            return SimpleDateFormat.parse(time);
        } catch (Exception e){
        }
        return null;
    }


    public static Date getFormatDate(String time) {
        SimpleDateFormat SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            return SimpleDateFormat.parse(time);
        } catch (Exception e){
        }
        return null;
    }

    /**
     * 计算年龄
     * @param birthday
     * @return
     */
    public static int getAgeByBirth(Date birthday){
        //Calendar：日历
        /*从Calendar对象中或得一个Date对象*/
        Calendar cal = Calendar.getInstance();
        /*把出生日期放入Calendar类型的bir对象中，进行Calendar和Date类型进行转换*/
        Calendar bir = Calendar.getInstance();
        bir.setTime(birthday);
        /*如果生日大于当前日期，则抛出异常：出生日期不能大于当前日期*/
        if(cal.before(birthday)){
            throw new IllegalArgumentException("The birthday is before Now,It's unbelievable");
        }
        /*取出当前年月日*/
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayNow = cal.get(Calendar.DAY_OF_MONTH);
        /*取出出生年月日*/
        int yearBirth = bir.get(Calendar.YEAR);
        int monthBirth = bir.get(Calendar.MONTH);
        int dayBirth = bir.get(Calendar.DAY_OF_MONTH);
        /*大概年龄是当前年减去出生年*/
        int age = yearNow - yearBirth;
        /*如果出当前月小与出生月，或者当前月等于出生月但是当前日小于出生日，那么年龄age就减一岁*/
        if(monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)){
            age--;
        }
        return age;
    }

    public static int getAgeByBirth(String birthday){
        Date date = getFormatDateStr(birthday);
        return getAgeByBirth(date);
    }

    public static Date dayStart(Date date){
        String dateStr = getFormatDate(date.getTime()) + " 00:00:00";
        return getFormatDate(dateStr);
    }
    public static Date dayEnd(Date date){
        String dateStr = getFormatDate(date.getTime()) + " 23:59:59";
        return getFormatDate(dateStr);
    }

    public static Date weekStart(Date date){
        // 获取指定日期所在周的第一天(周一)
        Calendar calendar = Calendar.getInstance();
        // 设置周一是第一天, getFirstDayOfWeek获取的默认是周日
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        // 设置指定日期
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        return calendar.getTime();
    }

    public static Date weekEnd(Date date){
        // 获取指定日期所在周的最后一天(周日)
        Calendar calendar = Calendar.getInstance();
        // 设置周一是第一天
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        // 设置指定日期
        calendar.setTime(date);
        // +0周一  +1周二  ...类推
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6);
        return calendar.getTime();
    }

    public static Date monthStart(Date date){
        // 获取指定日期所在月的第一天
        Calendar calendar = Calendar.getInstance();
        // 设置指定日期
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        return calendar.getTime();
    }

    public static Date monthEnd(Date date){
        // 获取指定日期所在月的最后一天
        Calendar calendar = Calendar.getInstance();
        // 设置指定日期
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        // 最后一天
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }

    public static String getDay() {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        String startDateString = format1.format(date);
        return startDateString;
    }

    /**
     * 获取明天日期
     * @return
     */
    public static String getNextDay() {
        long da = System.currentTimeMillis()+24*60*60*1000;
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        return format1.format(new Date(da));
    }

    public static String formatDateTime(Date date) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startDateString = format1.format(date);
        return startDateString;
    }

    /**
     * 是否超过指定日期
     * @param time
     * @return true 超过 false没超过
     */
    public static boolean checkDate(String time) {
        Date date = getFormatDateStr(time);
        return date.getTime() < System.currentTimeMillis();
    }
}
